package com.wxx.config;

import com.wxx.interceptior.LoginInterceptior;
import com.wxx.interceptior.PermissionsInterceptior;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class WebMvcConfigerer  implements WebMvcConfigurer   {
    // 拦截器配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

//        匹配规则
//        /admin/**：匹配 /admin/ 下的所有路径
//        /secure/*：只匹配 /secure/user，不匹配 /secure/user/info

        //登录验证
        registry.addInterceptor(new LoginInterceptior()).addPathPatterns("/bg/**")
                .excludePathPatterns("/bg/dicUser/login")
                .excludePathPatterns("/bg/dicUser/exit")
                //释放静态资源
                .excludePathPatterns("/templates/**");

        //权限拦截器 可以加入多个 addPathPatterns
        registry.addInterceptor(new PermissionsInterceptior()).addPathPatterns("/bg/**")
                .excludePathPatterns("/bg/**/find**")
                .excludePathPatterns("/bg/**/add*")
                .excludePathPatterns("/bg/**/edit*")
                .excludePathPatterns("/bg/dicUser/login")
                .excludePathPatterns("/bg/dicUser/exit");
    }

    //静态资源配置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //需要配置1：--- 需要告知系统，这是要被当成静态文件的！  设置内部静态资源
        //第一个方法设置访问路径前缀，第二个方法设置资源路径
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");

        //  获取与jar同级目录下的upload文件夹  设置与jar同级静态资源配置
        ApplicationHome h = new ApplicationHome(this.getClass());
        // D:\idea\springboot2.x\target  upload 跟 项目jar平级
        String path = h.getSource().getParent();
        String realPath = path + "/upload/";
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+realPath);
    }

    //跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //允许所有跨域访问
        registry.addMapping("/**");
    }

    //跳转页面设置
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //首次登录跳转的页面
        registry.addViewController("/").setViewName("login");
    }


}
